import { ref } from 'vue'
import { userId, opt } from './user'
import axios from 'axios'
import download from "downloadjs"


//图片的URL
export const imgUrl = ref('C:\vsCode_code\chatSoftwarePicture.jpg')

//上传图片
export function uploadImg() {
    const formData = new FormData()
    formData.append('userId', userId.value)
    formData.append('head', imgUrl.value)
    axios.post("https://47.120.69.214:8787/user/uploadHead", formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
        .catch(error => {
            console.log(error)
        })
}

import img from '@/assets/1.jpg'
//拼接文件路径
export const splicingUrl = (url) => {
    if(opt.value === 0)
         return "https://47.120.69.214:8787/" + url
    else
        return img
}

//下载文件
export function downloadFile(url, fileName) {
    // download(url, fileName)

    console.log(url)
    console.log(fileName)
    // const xhr = new XMLHttpRequest();
    // xhr.open('GET', url, true);
    // xhr.responseType = 'blob';
    // xhr.onload = (e) => {   
    //     const url = window.URL.createObjectURL(xhr.response);
    //     // 调用上面封装的函数
    //     downloadByLink({url, fileName, target});
    // };
    // xhr.onerror = () => {};
    // xhr.send();

    // axios({
    //     url: url,
    //     method: 'GET',
    //     responseType: 'blob',
    //     fileName : fileName
    // }).then(response => response.blob())
    // .then(blob=> {
    //     console.log(blob);
    // })

    axios({
        url: url,
        method: 'get',
        responseType: 'blob'
    }).then(response => response.data)
        .then(data => {
            let a = document.createElement("a")
            a.href = URL.createObjectURL(data)
            a.download = fileName
            a.click()
        })
}

export const isAudio = ref(false)

